package com.pets.controller;


import com.pets.pojo.Users;
import com.pets.service.IUsersService;
import com.pets.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Gikx
 * @since 2024-04-04
 */
@RestController
@RequestMapping("/users")
@Api(tags = "用户管理接口")
public class UsersController {

    @Autowired
    private IUsersService usersService;

    @ApiOperation("用户登陆")
    @GetMapping
    public Result login(@RequestParam String username, @RequestParam String password,HttpSession session) {
        return usersService.login(username, password,session);
    }

    @ApiOperation("退出登陆")
    @PostMapping("/loginout")
    public Result loginout(HttpSession session) {
        session.removeAttribute("user");
        return Result.success("退出成功！");
    }

    @ApiOperation("用户注册与创建用户")
    @PostMapping
    public Result register(@RequestBody Users users) {
        return usersService.register(users);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return usersService.deleteById(id);
    }

    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody Users users) {
        return usersService.update(users);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Integer id) {
        return usersService.selectById(id);
    }

    @ApiOperation("分页查询用户")
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @ApiParam(value = "账号搜索")@RequestParam(required = false) String username,
                       @ApiParam(value = "昵称搜索")@RequestParam(required = false) String nickname,
                       @ApiParam(value = "用户类型筛选")@RequestParam(required = false) Integer userType,
                       @ApiParam(value = "性别筛选")@RequestParam(required = false) String gender) {
        return usersService.selectPage(page, pageSize, username, nickname,userType, gender);
    }

}
